package com.iweb.project.Category.servlet;
import com.alibaba.fastjson2.JSONObject;
import com.iweb.project.Category.model.Category;
import com.iweb.project.Category.service.CategoryService;
import com.iweb.project.Category.service.CategoryServiceImpl;
import com.iweb.project.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "*.dishes")
public class CategoryServlet extends HttpServlet {

    CategoryService categoryService = new CategoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String path = req.getServletPath();
        switch (path) {
            case "/toList.dishes":
                toList(req, resp);
                break;
            case "/selectByPage.dishes":
                selectByPage(req, resp);
                break;
            case "/toEdit.dishes":
                toEdit(req, resp);
                break;
            case "/addOrUpdate.dishes":
                addOrUpdate(req, resp);
                break;
            case "/queryById.dishes":
                queryById(req, resp);
                break;
            case "/deleteById.dishes":
                deleteById(req, resp);
                break;
            default:
                break;
        }
    }
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        if (req.getSession().getAttribute("manager")==null){
//            resp.getWriter().print("");
//            return;
//        }
        String pageNo = req.getParameter("pageNo");
        int pageNoInt = Integer.parseInt(pageNo);
        String likeValue = req.getParameter("likeValue");
        PageModel pageModel = new PageModel();
        pageModel.setPageNo(pageNoInt);
        pageModel.setLikeValue(likeValue);
        List<Category> categoryList = categoryService.selectByPage(pageModel);
        pageModel.setRecordList(categoryList);
        String result = JSONObject.toJSONString(pageModel);
        resp.getWriter().print(result);
    }

    /*
    跳转去查询页面
     */
    private void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/dishes/dishes_selectByPage.html")
                .forward(req, resp);
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("saveDishesFlag", "1");
        String cid = req.getParameter("cid");
        resp.sendRedirect(req.getContextPath() + "/dishes/dishes_toAdd.html?cid=" + cid);
    }

    private void addOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        HttpSession session = req.getSession();
        Object o = session.getAttribute("saveDishesFlag");
        if (o != null) {
            session.removeAttribute("saveDishesFlag");
            String dish_name = req.getParameter("dish_name");
            String dish_type = req.getParameter("dish_type");
            String dish_description = req.getParameter("dish_description");
            String price = req.getParameter("price");
            String cid = req.getParameter("cid");
            if (cid == null || "".equals(cid)) {
                //新增
                Category category = new Category(null, dish_name, dish_type, dish_description, Integer.valueOf(price));
                categoryService.addOne(category);
            } else {
                //修改
                Category category = new Category(Integer.valueOf(cid), dish_name, dish_type, dish_description, Integer.valueOf(price));
                categoryService.updateOne(category);
            }
            resp.sendRedirect(req.getContextPath() + "/toList.dishes?pageNo=1&likeValue=");
        } else {
            resp.sendRedirect(req.getContextPath() + "/token.html");
        }
    }

    private void queryById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        Category category = categoryService.queryById(cid);
        String result = JSONObject.toJSONString(category);
        resp.getWriter().print(result);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        categoryService.deleteById(cid);
        resp.sendRedirect(req.getContextPath() + "/toList.dishes?pageNo=1&likeValue=");
    }
}
